package com.fuhao.email.service;

import javax.security.auth.Subject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.fuhao.adapter.MessageAdapter;

import lombok.extern.slf4j.Slf4j;

//处理第三方发送邮件
@Slf4j
@Service
public class EmailService implements MessageAdapter {
	
	@Autowired
	private JavaMailSender javaMailSender;
	
	@Value("${msg.subject}")
	private String Subject;//主题
	@Value("${msg.text}")
	private String test;//内容
	@Value("${email.account}")
	private String account;//发送端的账号
	@Override
	public void sendMsg(JSONObject body) {
		// 处理发送邮件
		String email=body.getString("email");
		if(StringUtils.isEmpty(email)){
			return ;
		}
		log.info("消息服务平台发送邮件:{}",email);
		//发送邮件
		SimpleMailMessage message = new SimpleMailMessage();
		//谁发送
		message.setFrom(account);
		//发给谁
		message.setTo(email);
		//发送的主提
		message.setSubject(Subject);
		//发送内容
		message.setText(test.replace("{}", email));
		//开始发送邮件
		javaMailSender.send(message);
		log.info("消息服务平台发送邮件:{}完成", email);
	}
}
